//
//  LoginViewController.swift
//  PlayWithXMPP_Swift
//
//  Created by admin on 15/1/27.
//  Copyright (c) 2015年 com.zhongyan. All rights reserved.
//

import UIKit

class LoginViewController: UIViewController, XMPPStreamDelegate {
    
    @IBOutlet weak var userNameTF: UITextField!
    
    @IBOutlet weak var passwordTF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupGesture()
        self.setupTextField()
        self.setupXMPPStream()
    }

    func setupTextField() {
        
        userNameTF.text = UserInfoHelper.getUserName()
        passwordTF.text = UserInfoHelper.getPassword()
        
 
    }
    
    func setupXMPPStream() {
        AppDelegate.shareInstance().xmppStream!.addDelegate(self, delegateQueue: dispatch_get_main_queue())
    }
    
    func setupGesture() {
        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("doGestureRecognizer"))
        self.view.addGestureRecognizer(tapGesture)
    }
    
    @IBAction func doRegisterButtonClicked(sender: AnyObject) {
        
    }

    @IBAction func doForgotPasswordButtonClicked(sender: AnyObject) {
        
    }
    
    @IBAction func doLoginButtonClicked(sender: AnyObject) {
        if userNameTF.text.isEmpty
        {
            println("请输入用户名")
            return
        }
        if passwordTF.text.isEmpty
        {
            println("请输入密码")
            return
        }
        println(userNameTF.text + "," + passwordTF.text)
        AppDelegate.shareInstance().xmppStream!.myJID = XMPPJID.jidWithString(userNameTF.text)
        AppDelegate.shareInstance().xmppStream!.hostName = "10.10.1.114"
        AppDelegate.shareInstance().xmppStream!.connectWithTimeout(XMPPStreamTimeoutNone, error: nil)
        
    }
    func doGestureRecognizer() {
        userNameTF.resignFirstResponder()
        passwordTF.resignFirstResponder()
    }
    
    func dismissLoginVC() {
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
            
        })
    }
    
    func xmppStreamDidConnect(sender: XMPPStream!) {
        println(__FUNCTION__, __LINE__);

        AppDelegate.shareInstance().xmppStream!.authenticateWithPassword(passwordTF.text, error: nil)
    }
    
    func xmppStream(sender: XMPPStream!, didSendMessage message: XMPPMessage!) {
        println(__FUNCTION__, __LINE__);
    }
    
    func xmppStream(sender: XMPPStream!, didReceivePresence presence: XMPPPresence!) {
        println(__FUNCTION__, __LINE__);
    }
    func xmppStream(sender: XMPPStream!, didReceiveMessage message: XMPPMessage!) {
        println(__FUNCTION__, __LINE__);
        println(message)
    }
    
    func xmppStream(sender: XMPPStream!, didNotAuthenticate error: DDXMLElement!) {
        println("登陆失败，用户名或者密码错误")
        println(error)
    }

    func xmppStreamDidAuthenticate(sender: XMPPStream!) {
        println(__FUNCTION__, __LINE__);
        
        UserInfoHelper.saveLoginStatus(true)
        UserInfoHelper.saveUserName(userNameTF.text)
        UserInfoHelper.savePassword(passwordTF.text)
        
        let presence:XMPPPresence = XMPPPresence()
        AppDelegate.shareInstance().xmppStream?.sendElement(presence)
        AppDelegate.shareInstance().xmppStream?.removeDelegate(self)
        self.dismissLoginVC()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    

}
